﻿namespace Dcms.BaseClass
{
    using Dcms.ToEmpower;
    using System;
    using System.Reflection;
    using System.Web.UI;

    public abstract class DcmsOperateModel
    {
        public DcmsOperateModel()
        {
            Dcms.ToEmpower.ToEmpower.ValidateAndRedirect();
        }

        public abstract void aInitSetValue(Control ctl, object oValue);
        public abstract void aSetControlValue(string strFieldID, PropertyInfo property, object model, Control ctl);
        public void InitControl(object model, ControlCollection Controls)
        {
            PropertyInfo[] arrProperty = model.GetType().GetProperties();
            foreach (PropertyInfo property in arrProperty)
            {
                foreach (Control ctl in Controls)
                {
                    string strControlID = (ctl.ID == null) ? "" : ctl.ID;
                    if (strControlID != "")
                    {
                        string strFieldID = strControlID.Remove(0, 3);
                        if (!strFieldID.StartsWith("_") && string.Compare(strFieldID, property.Name, true) == 0) this.aInitSetValue(ctl, property.GetValue(model, null));
                    }
                }
            }
        }

        public void UpdateModelByControls(object model, ControlCollection Controls)
        {
            PropertyInfo[] arrProperty = model.GetType().GetProperties();
            foreach (PropertyInfo property in arrProperty)
            {
                foreach (Control ctl in Controls)
                {
                    string strControlID = (ctl.ID == null) ? "" : ctl.ID;
                    if (strControlID != "")
                    {
                        string strFieldID = strControlID.Remove(0, 3);
                        if (!strFieldID.StartsWith("_")) this.aSetControlValue(strFieldID, property, model, ctl);
                    }
                }
            }
        }
    }
}

